//
//  ViewController.m
//  11208
//
//  Created by student on 16/3/3.
//  Copyright © 2/Users/student/Desktop/素材及练习截图/11208/cars_total.plist016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSArray *carArr;
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *path=[[NSBundle mainBundle]pathForResource:@"cars_total" ofType:@"plist"];
    self.carArr=[[NSArray alloc]initWithContentsOfFile:path];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.carArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary *dic=self.carArr[section];
    NSArray *arr=dic[@"cars"];
    return arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    NSDictionary *dic=self.carArr[indexPath.section];
    NSArray *arr=dic[@"cars"];
    cell.textLabel.text=arr[indexPath.row][@"name"];
    cell.imageView.image=[UIImage imageNamed:arr[indexPath.row][@"icon"]];
    NSLog(@"%@",cell.imageView.image);
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.carArr[section][@"title"];
}

- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    NSMutableArray *mArr=[[NSMutableArray alloc]init];
    for (NSDictionary *dic in self.carArr) {
        [mArr addObject:dic[@"title"]];
    }
    return mArr;
}

@end
